	    function openConnection(){
	        initConnection();
	        $('#dlg').dialog('open').dialog('center').dialog('setTitle','数据库连接');
	        $('#fm').form('clear');
	    }
	    
	    function openConnectionCreate(){
	    	$('#dlg').dialog('close');
	        $('#dlg1').dialog('open').dialog('center').dialog('setTitle','创建数据库连接');
	        $('#createConnection').form('clear');
	    }
	    
	    function deleteConnection(){
	    	 $.messager.confirm('Confirm', '你确定要删除这条记录吗?', function(r){
     			if (r){
	    			indexOperator('dbConnection/delete.json',{ 'id':$('#initConnection').combobox('getValue')},'');
     			}
     		});
     	}
	    
	    function openSaveSql(){
	        $('#savesql').dialog('open').dialog('center').dialog('setTitle','保存查询语句');
	        $('#savesqlForm').form('clear');
	    }
	    
	    function initConnection(){
	    	$('#initConnection').combobox({
	    	    url:'dbConnection/findAll.json',
	    	    valueField:'id',
	    	    textField:'showname'
	    	});
	    }
	
	    function connection(){
	        $('#fm').form('submit',{
	            url: "sqlStatement/connection.json",
	            type: 'post',
	            onSubmit: function(){
	                return $(this).form('validate');
	            },
	            success: function(result){
	                if (result){
	                    $('#dlg').dialog('close');        // close the dialog
	                	initTableNamesAndSql();
	                	addPanel();
	                } else {
	                    $.messager.show({
	                        title: 'Error',
	                        msg: result.message
	                    });
	                }
	            }
	        });
	    }
	    
	    
	    function saveConnection(){
	    	$.ajax({
	            url: "dbConnection/save.json",
	            type: 'post',
	            data:{'showname':$('#showname').val(),'username':$('#username').val(),'password':$('#password').val(),'url':$('#url').val(),'driverClass':$('#driverClass').val(),'validationQuery':$('#validationQuery').val(),'sort':$('#sort').val()},
	            async:false,
	            onSubmit: function(){
	                return $(this).form('validate');
	            },
	            success: function(result){
	                    $('#dlg1').dialog('close');        // close the dialog
	            }
	        });
	    }

   //  数据库连接 end 
   
function sqlFormSubmit(){
	$.ajax({
		url: "sqlStatement/execute.json",
		data:{'id':$('#fm input[name=id]').val(),'sql':$('#'+$('#tt').tabs('getSelected').attr('id')+'sql').val()},
	    success:function(result){
	    	var columns = new Array();
	    	var length =  result.length;
	    	if(length > 0){
	    		 for(var name in result[0]){       
	    			columns.push({field:name,title:name,width:100});
	    		 }
	    	}
	    	showResult(columns,result);
	    }
	})
}

function showResult(columns,data){
	$('#sqlTable').datagrid({
	    columns:[columns],
	    data:data

	});
}

function initTableNamesAndSql(){
	findSaveSql();
	findTableNames();
}

function findSaveSql(){
	$('#sqlStatement').accordion({
		onSelect:function(title,index){
			 	$('#'+$('#tt').tabs('getSelected').attr('id')+'sql').textbox('setValue',$('#sqlStatement').accordion('getSelected').text());
		 }
	});	
	$.ajax({
        url: 'sqlStatement/findAll.json',
        type: 'post',
        data:{},
        async:false,
        success: function(result){
            $.each(result,function(i,value){
                $('#sqlStatement').accordion('add',{
            		 title:value.name,
            		 content:'<div style="padding:10px">'+value.content+'</div>',
            		 selected: false,
            		 tools:[{
                         iconCls:'icon-cancel',
                         handler:function(){
                        	 $.messager.confirm('Confirm', '你确定要删除这条记录吗?', function(r){
                        			if (r){
                        				indexOperator('sqlStatement/delete.json',{'id':value.id},function(){$('#sqlStatement').accordion('remove',i)});
                        			}
                        		});
                         }
        			 }]
            	});
            });
        }
    });
}

function indexOperator(url,data,fun){
	 $.ajax({
	        url: url,
	        data:data,
	        async:false,
	        success: function(result){
	        	fun(); 
	        }
	 });
}

function findTableNames(){
	$('#tableNames').tree({
	    url: "sqlStatement/getAllTableName.json",
	    queryParams:{'id':$('#fm input[name=id]').val()},
	    lines: true ,
	    async:false,
	    onDblClick: function(node){
			$('#'+$('#tt').tabs('getSelected').attr('id')+'sql').textbox('setValue',($('#'+$('#tt').tabs('getSelected').attr('id')+'sql').textbox('getText')+" "+node.text));
		},
		onContextMenu: function(e, node){
			e.preventDefault();
			// select the node
			$('#tableNames').tree('select', node.target);
			// display context menu
			$('#mm').menu('show', {
				left: e.pageX,
				top: e.pageY
			});
		}
	});
}

function saveSql(){
	$.ajax({
        url: 'sqlStatement/save.json',
        type: 'post',
        data:{'name':$('#saveSqlName').val(),'memo':$('#saveSqlMemo').val(),'content':$('#'+$('#tt').tabs('getSelected').attr('id')+'sql').val(),'dbConnectionId':$('#fm input[name=id]').val()},
        async:false,
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            	findSaveSql();
                $('#savesql').dialog('close');        // close the dialog
        }
    });
}

function insertSql(){
	var selectTable = $('#tableNames').tree('getSelected') ;
	var sql = 'insert into '+ selectTable.text ;
	var field =  getSqlField(selectTable.children);
	sql += ' ('+field+') values ('+field+')';
	$('#'+$('#tt').tabs('getSelected').attr('id')+'sql').textbox('setValue',sql);
}

function updateSql(){
	var selectTable = $('#tableNames').tree('getSelected') ;
	var sql = 'update ' + selectTable.text +' set ';
	var field =  '';
	for (var i = 0; i < children.length; i++) {
		field += "`"+ children[i].text + "`"+'='+ "`"+children[i].text + "`";
		if( i != (children.length-1)){
			field += ',' 
		}
	}
	sql += field ;
	$('#'+$('#tt').tabs('getSelected').attr('id')+'sql').textbox('setValue',sql);
}

function selectSql(){
	var selectTable = $('#tableNames').tree('getSelected') ;
	var field =  getSqlField(selectTable.children);
	var sql = 'select  ' +field +' from '+ selectTable.text ;
	$('#'+$('#tt').tabs('getSelected').attr('id')+'sql').textbox('setValue',sql);
}

function deleteSql(){
	var selectTable = $('#tableNames').tree('getSelected') ;
	var field =  getSqlField(selectTable.children);
	var sql = 'delete from  '+ selectTable.text + ' where id = ' ;
	$('#'+$('#tt').tabs('getSelected').attr('id')+'sql').textbox('setValue',sql);
}

function getSqlField(children){
	var field = '';
	for (var i = 0; i < children.length; i++) {
		field += "`"+children[i].text +"`";
		if( i != (children.length-1)){
			field += ',' 
		}
	}
	return field;
}

var index = 0;
function addPanel(){
	index++;
    $('#tt').tabs('add',{
    	id:'tab'+index,
        title: '窗口'+index,
        content: '<div style="margin-bottom:10px;height:100%"><input class="easyui-textbox" id="tab'+index+'sql" name="sql'+index+'" style="width:100%;height:100%" data-options="multiline:true"></input></div>',
        closable: true
    });
   
}
function removePanel(){
    var tab = $('#tt').tabs('getSelected');
    if (tab){
        var index = $('#tt').tabs('getTabIndex', tab);
        $('#tt').tabs('close', index);
    }
}
//数据库表操作按钮